package org.kolonitsky.css.runner.maven;

import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.kolonitsky.css.runner.Runner;
import org.kolonitsky.css.runner.Settings;

import java.io.*;
import java.net.JarURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.zip.ZipException;

import static org.apache.commons.io.FileUtils.copyDirectory;
import static org.apache.commons.io.FileUtils.getFile;
import static org.apache.commons.io.FileUtils.getTempDirectory;

/**
 * @author Alex.Kolonitsky
 * @goal run
 */
public class RunMojo extends AbstractMojo implements Settings {

    /**
     * @parameter default-value="${project.build.sourceDirectory}"
     */
    private File cssInputFolder;

    /**
     * @parameter default-value="${project.build.directory}/cssdoc"
     */
    private File htmlOutputFolder;

    /**
     * @parameter default-value="${project.build.directory}/cssdoc/xml"
     */
    private File xmlOutputFolder;

    /**
     * @parameter
     */
    private File htmlTemplateFolder;

    /**
     * @parameter
     */
    private File xslt;

    /**
     * @parameter default-value="${project.build.directory}/cssdoc-template"
     */
    private File extractDir;

    public File cssInputFolder() {
        return cssInputFolder;
    }

    public File htmlOutputFolder() {
        return htmlOutputFolder;
    }

    public File xmlOutputFolder() {
        return xmlOutputFolder;
    }

    public File htmlTemplateFolder() {
        if (htmlTemplateFolder == null) {
            extract();
            htmlTemplateFolder = extractDir;
        }
        return htmlTemplateFolder;
    }

    private void extract() {
        URL url = resource(DEFAULT_TEMPLATE);
        try {
            JarURLConnection connection = (JarURLConnection) url.openConnection();

            JarFile jarFile = connection.getJarFile();
            Enumeration<JarEntry> entries = jarFile.entries();
            while (entries.hasMoreElements()) {
                JarEntry jarEntry = entries.nextElement();
                if (!jarEntry.getName().startsWith(DEFAULT_TEMPLATE)) {
                    continue;
                }

                File distFile = getFile(extractDir, jarEntry.getName().replaceFirst(DEFAULT_TEMPLATE, ""));
                if (jarEntry.isDirectory()) {
                    FileUtils.forceMkdir(distFile);
                } else {
                    FileUtils.copyInputStreamToFile(jarFile.getInputStream(jarEntry), distFile);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public File xslt() {
        if (xslt == null) {
            xslt = getFile(htmlOutputFolder(), "xslt");
        }
        return xslt;
    }

    private URL resource(String name) {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        return classLoader.getResource(name);
    }

    public void execute() throws MojoExecutionException, MojoFailureException {
        Runner runner = new Runner(this);
        runner.run();
    }
}
